package mapper;

import model.SysUsers;
import model.dto.SysUsersDto;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface SysUsersMapper {
    @Select("<script>select u.*,ifnull(d.name,'没有部门') dname from sys_users u left join department d on u.department_id = d.id " +
            "<where>" +
            "<if test=\"userName!=null and userName!=''\"> and user_name like \"%\"#{userName}\"%\"</if>" +
            "<if test=\"userType>0\"> and user_type = #{userType}</if>" +
            "</where> order by created_time desc" +
            " limit #{page},#{limit}</script>")
    List<SysUsers> findAll(SysUsersDto dto);

    @Select("<script>select count(1) from sys_users"+
            "<where>" +
            "<if test=\"userName!=null and userName!=''\"> and user_name like \"%\"#{userName}\"%\"</if>" +
            "<if test=\"userType>0\"> and user_type = #{userType}</if>" +
            "</where> </script>")
    int getCount(SysUsersDto dto);

    @Select("select * from sys_users where id = #{id}")
    SysUsers findById(int id);

    @Insert("insert into sys_users ( department_id, user_type, head_url, account, user_name, password, salt, created_user_id, status)\n" +
            "values (#{departmentId},#{userType},#{headUrl},#{account},#{userName},#{password},#{salt},#{createdUserId},#{status})")
    int add(SysUsers users);

    @Select("select * from sys_users where department_id=#{id}")
    List<SysUsers> findByDepartmentId(int id);

    @Select("select count(*) from sys_users where account = #{account}")
    int checkAccount(String account);

    @Select("select count(*) from sys_users where user_name = #{userName}")
    int checkUserName(String userName);

    @Select("select * from sys_users where account=#{account} and status=0")
    SysUsers login(SysUsers users);

    @Update("update sys_users set department_id=#{departmentId},user_type=#{userType},account=#{account},user_name=#{userName},password=#{password},status=#{status} where id = #{id}")
    int edit(SysUsers users);

    @Delete("delete from sys_users where id = #{id}")
    int del(int id);

    @Update("update sys_users set status = abs(status-1) where id = #{id}")
    int changeStatus(int id);



}
